from django.contrib import auth
from django.db import models

# Create your models here.

from django.contrib.auth.models import User, AbstractBaseUser, PermissionsMixin, BaseUserManager
from django.contrib.auth.hashers import make_password


class UserStatusChoice(models.IntegerChoices):
    ACTIVED = 1
    UNACTIVE = 2
    LOCKED = 3

class OAUserManager(BaseUserManager):
    use_in_migrations = True

    def _create_user(self, realname, email, password, **extra_fields):
        """
        Create and save a user with the given username, email, and password.
        """
        if not realname:
            raise ValueError("必须设置真实姓名！")
        user = self.model(realname=realname, email=email, **extra_fields)
        user.password = make_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, realname, email=None, password=None, **extra_fields):
        extra_fields.setdefault("is_staff", False)
        extra_fields.setdefault("is_superuser", False)
        return self._create_user(realname, email, password, **extra_fields)

    create_user.alters_data = True

    async def acreate_user(self, realname, email=None, password=None, **extra_fields):
        extra_fields.setdefault("is_staff", False)
        extra_fields.setdefault("is_superuser", False)
        return await self._acreate_user(realname, email, password, **extra_fields)

    acreate_user.alters_data = True

    def create_superuser(self, realname, email=None, password=None, **extra_fields):
        extra_fields.setdefault("is_staff", True)
        extra_fields.setdefault("is_superuser", True)
        extra_fields.setdefault('status', UserStatusChoice.ACTIVED)

        if extra_fields.get("is_staff") is not True:
            raise ValueError("超级用户必须设置 is_staff=True.")
        if extra_fields.get("is_superuser") is not True:
            raise ValueError("超级用户必须设置 is_superuser=True.")

        return self._create_user(realname, email, password, **extra_fields)

    create_superuser.alters_data = True

    async def acreate_superuser(
        self, username, email=None, password=None, **extra_fields
    ):
        extra_fields.setdefault("is_staff", True)
        extra_fields.setdefault("is_superuser", True)

        if extra_fields.get("is_staff") is not True:
            raise ValueError("Superuser must have is_staff=True.")
        if extra_fields.get("is_superuser") is not True:
            raise ValueError("Superuser must have is_superuser=True.")

        return await self._acreate_user(username, email, password, **extra_fields)

    acreate_superuser.alters_data = True


# 重写User模型
class OAUser(AbstractBaseUser, PermissionsMixin):

    realname = models.CharField(max_length=150,unique=False)
    email = models.EmailField(unique= True, blank=False)
    telephone = models.CharField(max_length=11,blank=True)
    is_staff = models.BooleanField(default=True)
    is_active = models.BooleanField( default=True)
    status = models.IntegerField(choices=UserStatusChoice, default=UserStatusChoice.UNACTIVE)
    date_joined = models.DateTimeField(auto_now_add=True)

    objects = OAUserManager()

    EMAIL_FIELD = "email"
    # USERNAME_FIELD 鉴权的
    USERNAME_FIELD = "email"
    # REQUIRED_FIELDS 必传字段
    REQUIRED_FIELDS = ["realname", 'password']



    def clean(self):
        super().clean()
        self.email = self.__class__.objects.normalize_email(self.email)

    def get_full_name(self):
        return self.realname

    def get_short_name(self):
        return self.realname


